<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <script type="text/javascript">
      var description                                                           //因为使用了FCKeditor，除了extjs本身管理，还需要额外赋值，要使用全局变量
      Ext.onReady(function(){
        var maintenanceId = <%=params[:maintenanceId]%>
        //时间选择模块
        var workDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/work/getAllWorks'}),
          autoLoad:true,
          reader:new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'work'
          }, [
            {name:'name'},                            //用作后面的displayField:
            {name:'id'}                                //用作后面的valueField:
          ])
        });
        var workIdCombo = new Ext.form.ComboBox({  //配合上面的departmentDS，完成动态生成的下拉框
          store: workDS,
          hiddenName:'work_id',                //显示时：这里写后台传送过来的字段，一旦和valueField相等，就进行显示。不相等为空。
          displayField:'name',              //store里面的值，用于显示在表面上的值
          valueField:'id',                  //store里面的值，和display联动
          typeAhead: true,
          allowBlank:false,
          disable:true,
          minChars:1,
          readOnly:false,
          anchor: '95%',                    //在容器中的时候，自动按照容器百分比进行调整，会覆盖宽度的属性
          mode: 'remote',
          fieldLabel:'工作选择',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'请选择工作.....',
          selectOnFocus:true
        });

        var teamDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/team/getAllTeams?for_cbb=true'}),
          autoLoad:true,
          reader:new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'team'
          }, [
            {name:'name'},                            //用作后面的displayField:
            {name:'id'}                                //用作后面的valueField:
          ])
        });
        var teamIdCombo = new Ext.form.ComboBox({  //配合上面的departmentDS，完成动态生成的下拉框
          store: teamDS,
          hiddenName:'team_id',                //显示时：这里写后台传送过来的字段，一旦和valueField相等，就进行显示。不相等为空。
          displayField:'name',              //store里面的值，用于显示在表面上的值
          valueField:'id',                  //store里面的值，和display联动
          typeAhead: true,
          allowBlank:false,
          disable:true,
          minChars:1,
          readOnly:false,
          anchor: '95%', 
          mode: 'remote',
          fieldLabel:'组队选择',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'请选择组队.....',
          selectOnFocus:true
        });

        var beginTimeField = new Ext.ux.form.DateTimeField({
          fieldLabel: '维护单元规定开工时间',
          width: 200,
          name: 'begin_time',
          id: 'begin_time',
          invalidText: "您输入的日期格式不对，格式应如：2010-01-01",
          allowBlank: false,
          blankText: "维护单元规定开工时间必须选择",
          emptyText: '请选择时间...'

        });


        //时间选择模块
        var finishTimeField = new Ext.ux.form.DateTimeField({
          fieldLabel: '维护单元规定完工时间',
          width: 200,
          name: 'finish_time',
          id: 'finish_time',
          invalidText: "您输入的日期格式不对，格式应如：2010-01-01",
          allowBlank: false,
          blankText: "维护单元规定完工时间必须选择",
          emptyText: '请选择时间...'
        });

<%#*var textarea = new Ext.form.TextArea({%>
<%#*xtype:'textarea',%>
<%#*fieldLabel:'维护单元备注',%>
<%#*id:'description',%>
<%#*width:755,%>
<%#*height:225,%>
<%#*allowBlank:true,%>
<%#*name:'description'%>
<%#*});%>

    var descriptionHtmlEditor = new Ext.form.HtmlEditor({
      fieldLabel: '内容',
      name: 'description',
      width:755,
      height:325
    })

    var editMaintenanceForm=new Ext.form.FormPanel({
      width:780,
      url:'/maintenance/update',
      labelAlign: 'top',
      buttonAlign:'center',
      bodyStyle: 'padding:5px',
      labelWidth:100,
      frame: true,
      reader:new Ext.data.JsonReader({
      },[
        {name:'id'},
        {name:'description'},
        {name:'begin_time'}
        ,{name:'finish_time'}
        ,{name:'work_id'}
        ,{name:'team_id'}
      ]),
      items:[
        {
          xtype: 'hidden',
          name:  'id',
          value: id
        }
        ,{layout:'column'
          ,items:[
            {columnWidth:.4,
              layout:'form',
              items:[{
                  xtype:'fieldset',
                  title:'任务与组队',
                  width:240,
                  buttonAlign: 'center',
                  autoHeight:true,
                  items:[workIdCombo,teamIdCombo]
                }]
            },
            {columnWidth:.33,
              layout:'form',
              items:[{
                  xtype:'fieldset',
                  title:'开工与完工时间',
                  width:240,
                  buttonAlign: 'center',
                  autoHeight:true,
                  items:[beginTimeField,finishTimeField]
                }]
            }
          ]
        }
        ,{layout:'column',labelAlign: 'top',                            //栏目，占据一整行
          items:[{columnWidth:1,layout:'form'
              ,items:[descriptionHtmlEditor]
            }]}
      ],
      buttons:[{
          text:'确认保存',
          handler: function(){
            if (editMaintenanceForm.form.isValid()) {
              <%#*this.setDisabled(true);                                       //设置按钮不可重复点击%>
              editMaintenanceForm.form.submit({
                method: 'post',
                success: function(result, response){
                  if (response.result.info == "success") {
                    //添加成功后，隐藏窗口，并刷新Grid
                    editMaintenanceWin.destroy();
                  }
                  else {
                    Ext.MessageBox.alert('消息', response.result.info);
                    <%#*this.setDisabled(false);                                //但发生错误返回时，提交按钮重新可以使用%>
                  }
                },
                failure: function(result, response){
                  Ext.MessageBox.alert('提示', result.responseText);
                }
              });
            }
            else {
              Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
            }
          }
        },{
          text:'取消',
          handler:function(){
            editMaintenanceWin.destroy();
          }
        }]
    });

    editMaintenanceForm.form.load({
      url: '/maintenance/edit',
      params: {
        id: maintenanceId
      },
      waitMsg : '正在载入数据...',
      success: function(form, action){
        var beginTime = action.result.data.begin_time;                      //load的时候，取得的值，由此语法即可使用，这里取出了deadline
        beginTime = beginTime.substr(0,19);
        beginTime = beginTime.replace(/\//g,"-")
        beginTimeField.setValue(beginTime);
        var finishTime = action.result.data.finish_time;                    //load的时候，取得的值，由此语法即可使用，这里取出了deadline
        finishTime = finishTime.substr(0,19);
        finishTime = finishTime.replace(/\//g,"-")
        finishTimeField.setValue(finishTime);
<%#*description = action.result.data.description                        //取得备注的值，并赋值到变量description上%>
      },
      failure: function(form, action){
        Ext.MessageBox.alert('提示信息', '信息加载失败');
      }
    });

    workIdCombo.store.reload();
    teamIdCombo.store.reload();
    editMaintenanceForm.render('editMaintenanceForm');

<%#*var oFCKeditor = new FCKeditor('description',755,325) ;     //注意，要放在渲染之后，否则会报错。因为Fckeditor是在页面渲染完毕后又添加的。%>
<%#*oFCKeditor.BasePath = "/javascripts/fckeditor/" ;      //根目录是Public，下面的javascripts/...%>
<%#*oFCKeditor.ToolbarSet = 'Default';%>
<%#*oFCKeditor.ReplaceTextarea() ;%>
  });

<%#*var editorInstance;                                                       //注意，要放在FormPanel外面。%>
<%#*function FCKeditor_OnComplete( instance ) {                               // FCKEditor初始化完成将调用此方法：@param {Object} editorInstance%>
<%#*editorInstance=instance;%>
<%#*setTimeout("editorInstance.SetHTML(description)",1000);                 //等待一秒后进行赋值，防止复制到旧的值%>
<%#*};%>
    </script>
  </head>
  <body>
    <div id="editMaintenanceForm"></div>
  </body>
</html>